<template>
  <el-container class='elheight'>
    <el-aside class='appTree'>
      <app-tree v-bind:tree-data="treeData" v-on:appTreeClick="treeClickHandle"></app-tree>
    </el-aside>
    <el-main>
      <el-container>
        <el-header class="dzy-main-header">
          <div class="dzy-custom-search-box">
            <div>
              账号：
              <el-input v-model="selectForm.username" clearable autocomplete="off" class="dzy-custom-input"></el-input>
            </div>
            <div>
              姓名：
              <el-input v-model="selectForm.name" clearable autocomplete="off" class="dzy-custom-input"></el-input>
            </div>
            <div>
              联系电话：
              <el-input v-model="selectForm.phone" clearable autocomplete="off" class="dzy-custom-input"></el-input>
            </div>
            <div>
              <el-button @click="searchHandleClick" type="primary" icon="el-icon-search">查询</el-button>
            </div>
          </div>
        </el-header>
        <el-main>
          <app-table
            :page-data="pageData"
            :table-columns="tableColumns"
            v-on:currentChange="handleCurrentChange"
            v-on:checkFlowHandle="integralChangeHandle">
          </app-table>
          <el-dialog :title="dialogTitle"
            :visible.sync="dialogFormVisible"
            :before-close="cancel" :close-on-click-modal="false" class="custom-dialog_style">
            <el-form :model="formData" :rules="rules" ref="formData">
              <el-form-item label="姓名：">
                <el-input v-model="formData.name" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="积分类型：" prop="scoreType">
                <el-select v-model="formData.scoreTypeId" clearable placeholder="请选择">
                  <el-option v-for="(item,index) in integralTypeList" :key="index" :label="item.name" :value="item.id"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="备注：">
                <el-input type="textarea" v-model="formData.remarks"></el-input>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="cancel">取 消</el-button>
              <el-button type="primary" @click="save('formData')">确 定</el-button>
            </div>
          </el-dialog>
        </el-main>
      </el-container>
    </el-main>
  </el-container>
</template>

<script>
import AppTable from '@/components/AppTable'
import AppTree from '@/components/common/AppTree'
// import {getUserDeptTree} from '@/utils/SessionDataUtil'
import {findPersonalIntegral, personalIntegralChange, findIntegralTypeList, getIntegralDeptTree} from '@/http/integralApi'
export default {
  data () {
    return {
      dialogFormVisible: false,
      pageData: {},
      dialogTitle: '',
      tableColumns: [
        { prop: 'username', label: '账号' },
        { prop: 'name', label: '姓名' },
        { prop: 'departmentName', label: '所属部门' },
        { prop: 'phone', label: '联系电话' },
        { prop: 'socre.grade', label: '积分' }
      ],
      formData: {},
      selectForm: {},
      rules: {
        scoreTypeId: [
          { required: true, message: '请选择积分类型', trigger: 'blur' }
        ]
      },
      integralTypeList: [],
      treeData: []
    }
  },
  components: {
    AppTable,
    AppTree
  },
  mounted () {
    this.getDeptTree()
    this.getTableData()
  },
  methods: {
    gradeFormatter (row, column) {
      if (!row.socre) return 0
      else return row.socre.grade
    },
    getDeptTree () {
      getIntegralDeptTree().then(resp => {
        let m = this
        if (resp.code && resp.code === '200') {
          m.treeData = resp.data
        }
      })
    },
    integralChangeHandle (data) {
      let currentSelectData = data.data[data.index]
      this.formData = {}
      this.$set(this.formData, 'name', currentSelectData.name)
      this.$set(this.formData, 'userId', currentSelectData.userId)
      this.dialogFormVisible = true
      this.dialogTitle = '积分变动'
      this.getIntegralTypeList()
    },
    getIntegralTypeList () {
      findIntegralTypeList('01').then(resp => {
        if (resp.code && resp.code === '200') {
          this.integralTypeList = resp.data
        }
      })
    },
    save (formName) {
      this.$refs[formName].validate((valid) => {
        if (!valid) {
          this.$alert('请填写正确表单格式', '', {
            confirmButtonText: '确定'
          })
          return ''
        } else {
          let m = this
          personalIntegralChange(this.formData).then(resp => {
            if (resp.code && resp.code === '200') {
              m.dialogFormVisible = false
              m.$message({
                type: 'success',
                message: '操作成功'
              })
              m.getTableData()
            } else {
              m.$message({
                type: 'info',
                message: resp.message
              })
              return ''
            }
          })
        }
      })
    },
    cancel () {
      this.dialogFormVisible = false
      this.formData = {}
    },
    getTableData (param) {
      findPersonalIntegral(param).then(resp => {
        if (resp.code && resp.code === '200') {
          this.pageData = resp.data
          this.pageData.isIntegralHandleInline = true
          this.pageData.handleLabel = '积分变动'
        } else {
          this.$message({
            type: 'info',
            message: resp.message
          })
          return ''
        }
      })
    },
    searchHandleClick () {
      this.selectForm.offset = 0
      this.getTableData(this.selectForm)
    },
    handleCurrentChange (val) {
      this.selectForm.offset = (val - 1) * 10
      this.getTableData(this.selectForm)
    },
    treeClickHandle (data) {
      this.selectForm.deptId = data.id
      this.selectForm.offset = 0
      this.getTableData(this.selectForm)
    }
  }
}
</script>
<style scoped>
.elheight {
  margin-left: 10px
}
</style>
